package com.taptech.doufu.ui.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.taptech.doufu.R;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.SharedPreferenceUtil;

/**
 * Created by longsiyang on 16/2/23.
 */
public class GiftAnimManager {
    Context mContext;
    RelativeLayout giftLayout;
    ImageView giftBackView , giftTopView;
    SimpleDraweeView giftView;
    TextView topTextView;

    AnimatorSet animatorSet;
    Animator giftViewAnim , giftBackAnim , giftTopAnim , giftTopTextAnim;

    private ViewStub mGiftGuideStub;
    private String giftDesc = "";
    private String giftId;

    public GiftAnimManager(Context context , View rootView ){
        mContext = context;
        giftLayout = (RelativeLayout) rootView.findViewById(R.id.gift_layout_id);
        giftView = (SimpleDraweeView) rootView.findViewById(R.id.gift_view);
        giftBackView = (ImageView) rootView.findViewById(R.id.gift_back_view);
        giftTopView = (ImageView) rootView.findViewById(R.id.gift_top_view);
        topTextView = (TextView) rootView.findViewById(R.id.gift_top_text);
        mGiftGuideStub = (ViewStub) rootView.findViewById(R.id.gift_guide_stub);
        initAnim();
    }

    public void initAnim(){
        giftViewAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_img);
        giftViewAnim.setTarget(giftView);
        giftBackAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_back);
        giftBackAnim.setTarget(giftBackView);
        giftTopAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_gift_top);
        giftTopAnim.setTarget(giftTopView);
        giftTopTextAnim = AnimatorInflater.loadAnimator(mContext, R.animator.gift_anim_top_text);
        giftTopTextAnim.setTarget(topTextView);

        animatorSet = new AnimatorSet();
        giftTopTextAnim.setDuration(1000);
        animatorSet.playTogether(giftViewAnim , giftBackAnim , giftTopAnim , giftTopTextAnim);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                try {
                    if (!giftDesc.equals("") && !SharedPreferenceUtil.getBoolean(mContext, giftId)) {
                        giftLayout.setVisibility(View.VISIBLE);
                        final View giftGuideVIew = mGiftGuideStub.inflate();
                        TextView descView = (TextView) giftGuideVIew.findViewById(R.id.gift_promat_info);
                        descView.setText(giftDesc);
                        ImageView koneImg = (ImageView) giftGuideVIew.findViewById(R.id.gift_end_end);
                        koneImg.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                giftGuideVIew.setVisibility(View.GONE);
                                giftLayout.setVisibility(View.GONE);
                                SharedPreferenceUtil.putBoolean(mContext,giftId,true);
                            }
                        });
                    }else {
                        giftLayout.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    public void animStart(String url, final String desc, final String gId, final String topText){
        giftLayout.setVisibility(View.VISIBLE);
        ImageManager.loadImage(giftView, url, 1.0f, new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                super.onFinalImageSet(id, imageInfo, animatable);
                giftDesc = desc;
                giftId = gId;
                giftLayout.setVisibility(View.VISIBLE);
                topTextView.setText(topText);
                animatorSet.start();
            }
        });


    }
}
